/*  yALB - Yet Another List Builder. Roster Editor for Wargames/tabletop games.
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "listsettings.h"
#include "ui_listsettings.h"
#include <QMessageBox>
#include <QStyle>
#include <QDesktopWidget>
#include <QDebug>
#include <QFileDialog>

ListSettings::ListSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ListSettings)
{
    ui->setupUi(this);
    setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), QDesktopWidget().availableGeometry()));
    setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    setFixedSize(size());
}

ListSettings::~ListSettings()
{
    delete ui;
}

void ListSettings::addGame(const FileAndName& gameFile, const QList<FileAndName>& armies) {
    if(gameArmies.contains(gameFile.file)) {
        qDebug() << gameFile.file << "already exists";
        return;
    }

    ui->gameBox->addItem(gameFile.name, QVariant(gameFile.file));
    gameArmies.insert(gameFile.file, armies);

    if(ui->gameBox->currentIndex() == ui->gameBox->count() -1)
        on_gameBox_currentIndexChanged(ui->gameBox->currentIndex());
}

uint ListSettings::getListPoints() const {
    return uint(ui->pointLimitBox->value());
}

QString ListSettings::getCurrentGameFile() const {
    return ui->gameBox->itemData(ui->gameBox->currentIndex()).toString();
}

QString ListSettings::getCurrentArmyFile() const {
    return ui->armyList->currentItem()->data(Qt::UserRole).toString();
}

QString ListSettings::getListToOpen() const {
    return listToOpen;
}

void ListSettings::setListPoints(uint points) {
    ui->pointLimitBox->setValue(points);
}

void ListSettings::setCurrentArmyFile(const QString& armyFile) {
    QString currentGame = ui->gameBox->itemData(ui->gameBox->currentIndex()).toString();
    FileAndName army;
    army.file = armyFile;
    int index = gameArmies.value(currentGame).indexOf(army);

    if(index > -1)
        ui->armyList->setCurrentRow(index);
}

void ListSettings::setCurrentGameFile(const QString& gameFile) {
    for(int i = 0; i<ui->gameBox->count(); ++i) {
        if(ui->gameBox->itemData(i).toString() == gameFile) {
            ui->gameBox->setCurrentIndex(i);
            ui->armyList->setCurrentRow(0);
            return;
        }
    }
}

void ListSettings::on_buttonBox_accepted()
{
    //if(listOptions_changingArmy == true) {
        //if(listNotSaved() == true){
            if(ui->gameBox->currentIndex() > -1) {
                if(ui->armyList->currentRow() > -1) {
                    listToOpen = "";
                    this->accept();
                } else {
                    QMessageBox::critical(this, tr("Error"), tr("You must choose an army"));
                }
            } else {
                QMessageBox::critical(this, tr("Error"), tr("You must choose a game"));
            }
        /*}
    } else {
        bool ok = false;
        listOptions_points = ui->limPuntBox->text().toUInt(&ok);
        if(ok == true) {
            if(ui->ListaEjercitos->selectedItems().count() > 0) {
                if(ui->EdicionBox->currentIndex() > -1) {
                    this->accept();
                } else {
                    QMessageBox::critical(this, tr("Error"), tr("Seleccione una edici�n"));
                }
            } else {
                QMessageBox::critical(this, tr("Error"), tr("Debe seleccionar un ej�rcito"));
            }

        } else {
            QMessageBox::critical(this, tr("Error"), tr("El l�mite de puntos debe ser un n�mero positivo"));
        }
    }*/
}

void ListSettings::on_buttonBox_rejected()
{
    this->reject();
}

void ListSettings::on_gameBox_currentIndexChanged(int index)
{
    ui->armyList->clear();
    if(index > -1) {
        QList<FileAndName> armies(gameArmies.value(ui->gameBox->itemData(index).toString()));

        for(int i = 0; i<armies.size(); ++i) {
            QListWidgetItem *item = new QListWidgetItem(armies.at(i).name);
            item->setData(Qt::UserRole, QVariant(armies.at(i).file));
            ui->armyList->addItem(item);
        }
    }
}

void ListSettings::on_openListButton_clicked()
{
    listToOpen = QFileDialog::getOpenFileName(0, QString(), QString(), tr("yALB List File (*.ylf)"));
    if(!listToOpen.isEmpty())
        this->accept();
}
